
package com.jf.cloud.product.controller.multishop;

import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.product.dto.SpuSupplierChangeLogDTO;
import com.jf.cloud.product.model.SpuSupplierChangeLog;
import com.jf.cloud.product.service.SpuSupplierChangeLogService;
import com.jf.cloud.product.vo.SpuSupplierChangeLogVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 商品供应商变更记录
 *
 * @author zz
 * @date 2022-06-22 17:14:22
 */
@RestController("multishopSpuSupplierChangeLogController")
@RequestMapping("/m/spu_supplier_change_log")
@Tag(name = "商品供应商变更记录")
public class SpuSupplierChangeLogController {

    @Autowired
    private SpuSupplierChangeLogService spuSupplierChangeLogService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "获取商品供应商变更记录列表" , description = "分页获取商品供应商变更记录列表")
    public ServerResponseEntity<PageVO<SpuSupplierChangeLogVO>> page(@Valid PageDTO pageDTO) {
        PageVO<SpuSupplierChangeLogVO> spuSupplierChangeLogPage = spuSupplierChangeLogService.page(pageDTO);
        return ServerResponseEntity.success(spuSupplierChangeLogPage);
    }

    @GetMapping
    @Operation(summary = "获取商品供应商变更记录" , description = "根据spuChangeId获取商品供应商变更记录")
    public ServerResponseEntity<SpuSupplierChangeLog> getBySpuChangeId(@RequestParam Long spuChangeId) {
        return ServerResponseEntity.success(spuSupplierChangeLogService.getBySpuChangeId(spuChangeId));
    }

    @PostMapping
    @Operation(summary = "保存商品供应商变更记录" , description = "保存商品供应商变更记录")
    public ServerResponseEntity<Void> save(@Valid @RequestBody SpuSupplierChangeLogDTO spuSupplierChangeLogDTO) {
        SpuSupplierChangeLog spuSupplierChangeLog = mapperFacade.map(spuSupplierChangeLogDTO, SpuSupplierChangeLog.class);
        spuSupplierChangeLog.setSpuChangeId(null);
        spuSupplierChangeLogService.save(spuSupplierChangeLog);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新商品供应商变更记录" , description = "更新商品供应商变更记录")
    public ServerResponseEntity<Void> update(@Valid @RequestBody SpuSupplierChangeLogDTO spuSupplierChangeLogDTO) {
        SpuSupplierChangeLog spuSupplierChangeLog = mapperFacade.map(spuSupplierChangeLogDTO, SpuSupplierChangeLog.class);
        spuSupplierChangeLogService.update(spuSupplierChangeLog);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除商品供应商变更记录" , description = "根据商品供应商变更记录id删除商品供应商变更记录")
    public ServerResponseEntity<Void> delete(@RequestParam Long spuChangeId) {
        spuSupplierChangeLogService.deleteById(spuChangeId);
        return ServerResponseEntity.success();
    }

    @PostMapping("/batchSync")
    @Operation(summary = "变更信息批量同步" , description = "变更信息批量同步")
    public ServerResponseEntity<Void> batchSync(@RequestBody List<SpuSupplierChangeLogDTO> spuSupplierChangeLog){
        Long shopId = AuthUserContext.get().getTenantId();
        spuSupplierChangeLogService.syncSpu(shopId,spuSupplierChangeLog);
        return ServerResponseEntity.success();
    }
}
